<?php
/**
 * This is the static class that is responsible to handle views in the OpenSocial container 
 * 
 * @package    gadgets
 * @subpackage feature
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
class GadgetsViews
{
  /** @var string the name of the variable used for the current view   */
  public static $currentViewVarName = 'currentView';
  /** @var string the name of the variable used for the supported views   */
  public static $supportedViewsVarName = 'supportedViews';
  
  /**
   * Returns the current view. 
   * @param boolean $initialize if the variable should be initialized
   * @return GadgetsView Returns the current view. 
   */
  public static function getCurrentView($initialize=false)
  {
  	OSConfig::importViews();
  	$currentView = new GadgetsView(new JSVariable(GadgetsViews::$currentViewVarName,"gadgets.views.getCurrentView();"));
  	if($initialize)
  	  echo $currentView->getVariable();
  	return $currentView;
  }
  
  /**
   * Attempts to navigate to this gadget in a different view.
   * @param GadgetsView $view
   * @return string HTML code 
   */
  public static function requestNavigateTo(GadgetsView $view,$opt_params=null,$appendable=false)
  {
  	OSConfig::importViews();
  	$html='';
  	if(!$view->isInit())
  	  $html = $view->init()."\n";
  	$name = $view->getVariable()->getName();
  	$html .= "gadgets.views.requestNavigateTo($name";
  	if($opt_params)
  	  $html.= ",$opt_params";
  	$html.=")";
  	return StringUtil::appendableString($html,$appendable);
  }
  
  /**
   * Returns a map of all the supported views. 
   *
   * @param boolean $initialize
   * @param boolean $appendable
   * @return GadgetsSupportedViews
   */
  public static function getSupportedViews($initialize=false,$appendable=false)
  {
  	OSConfig::importViews();
  	$supportedViews = new GadgetsSupportedViews(GadgetsViews::$supportedViewsVarName);
  	if($initialize)
  	  echo $supportedViews->initialize($initialize,$appendable);
  	return $supportedViews;
  }
  
  /**
   * Returns the parameters passed into this gadget for this view.
   *
   * @param boolean $appendable
   * @return Map.<String, String>
   */
  public static function getParams($appendable=false)
  {
  	OSConfig::importViews();
  	return StringUtil::appendableString("gadgets.views.getParams()",$appendable);
  }
}